import { getStream } from '../Core/connect'
import * as MathUtils from '../Math/MathUtils'

export const TransformlContral = {
    // 平移
    startEditModel(uuid:string) {
        const stream = getStream()
        const str = {transformlFunction: 'startMoveModel',ids:uuid}
        stream.emitUIInteraction(str)
    },
    // 缩放
    startScaleModel(uuid:string) {
        const stream = getStream()
        const str = {transformlFunction: 'startScaleModel',ids:uuid}
        stream.emitUIInteraction(str)
    },
    // 旋转
    startRotateModel(uuid:string) {
        const stream = getStream()
        const str = {transformlFunction: 'startRotateModel',ids:uuid}
        stream.emitUIInteraction(str)
    },
    // 开启监听
    startLister(uuid:string,fun:Function){
        const sessionId = MathUtils.generateUUID()
        const stream = getStream()
        const str = {transformlFunction: 'startLister',sessionId:sessionId,ids:uuid}
        stream.emitUIInteraction(str)
        const startLister = (data: any) => {
            const returnData = JSON.parse(data)
            if(returnData.sessionId === sessionId){
                console.log('最新位置',returnData)
                fun(data)
            }
        }
        stream.addResponseEventListener(`startLister`, startLister)
    },
    // 落地：targetTag在ue中没有调用，传任意值
    fallGround(uuid:string) {
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const str = {transformlFunction: 'fallGround',ids:uuid,sessionId:sessionId}
            stream.emitUIInteraction(str)
            const getGroundPosition = (data: any) => {
                const returnData = JSON.parse(data)
                console.log('地球位置',data)
                if(returnData.sessionId === sessionId){
                    console.log('地球位置',returnData)
                    stream.removeResponseEventListener(`getGroundPosition`)
                    resolve(returnData)
                }
            }
            stream.addResponseEventListener(`getGroundPosition`, getGroundPosition)
        })
    },
    // 结束编辑
    stopEditModel() {
        const stream = getStream()
        const str = {transformlFunction: 'stopEditModel'}
        stream.emitUIInteraction(str)
    }
}